package com.winapi.project.service.impl.inner;

import com.winapi.project.model.vo.InterfaceInfoVO;
import com.winapi.project.model.vo.OrderVO;
import com.winapi.project.service.InterfaceInfoService;
import com.winapi.project.service.OrderService;
import com.winapi.project.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author yyzhang
 * @version 1.0
 */
@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Resource
    private InterfaceInfoService interfaceInfoService;
    @Resource
    private OrderService orderService;
    /**
     * 生成调用次数前三多的接口数据表
     * @param response
     */
    @Override
    public void topInvokeInterfaceInfoExcel(HttpServletResponse response){
               try {
                   InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("template/调用次数排名前三的接口.xlsx");
                   XSSFWorkbook excel = new XSSFWorkbook(in);
                    XSSFSheet sheet = excel.getSheet("Sheet1");
                    //根据调用查看调用接口次数前三的service，获取对应的行数
                   List<InterfaceInfoVO> interfaceInfoVOS = interfaceInfoService.topNumber();
                   int i=2;
                   //循环写入数据
                   for(InterfaceInfoVO vo:interfaceInfoVOS){
                       XSSFRow row = sheet.createRow(i);
                       i++;
                       row.createCell(0).setCellValue(vo.getName());
                       row.createCell(1).setCellValue(vo.getDescription());
                       row.createCell(2).setCellValue(vo.getUrl());
                       row.createCell(3).setCellValue(vo.getRequestHeader());
                       row.createCell(4).setCellValue(vo.getResponseHeader());
                       row.createCell(5).setCellValue(vo.getStatus());
                       row.createCell(6).setCellValue(vo.getMethod());
                       row.createCell(7).setCellValue(vo.getUserId());
                       row.createCell(8).setCellValue(vo.getCreateTime());
                       row.createCell(9).setCellValue(vo.getUserRequestParams());
                       row.createCell(10).setCellValue(vo.getTotalNum());
                   }
                   //文件输出地址
                   FileOutputStream out = new FileOutputStream(new File("D://info.xlsx"));
                   excel.write(out);
                   //关闭资源
                   out.close();
                   excel.close();

               } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void topBuyInterfaceInfoExcel(HttpServletResponse response) {
        try {
            InputStream in =  Thread.currentThread().getContextClassLoader().getResourceAsStream("template/购买次数的前五接口.xlsx");
            XSSFWorkbook excel = new XSSFWorkbook(in);
            XSSFSheet sheet = excel.getSheet("Sheet1");
            //根据调用查看购买接口次数前5的service，获取对应的行数
            List<OrderVO> orderVOList = orderService.listTopBuyInterfaceInfo();
            int i=2;
            for(OrderVO vo:orderVOList){
                XSSFRow row = sheet.createRow(i);
                i++;
                row.createCell(0).setCellValue(vo.getName());
                row.createCell(1).setCellValue(vo.getNumber());
                row.createCell(2).setCellValue(vo.getPrice());
                Double total=vo.getPrice()*vo.getNumber();
                row.createCell(3).setCellValue(total);
            }
            //文件输出地址
            FileOutputStream out = new FileOutputStream(new File("D://buy.xlsx"));
            excel.write(out);
            //关闭资源
            out.close();
            excel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
